package com.luyuan.crm.user.web.validator;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.springframework.stereotype.Component;

import com.sun.faces.util.MessageFactory;

@Component("passwordCompareValidator")
public class PasswordCompareValidator implements Validator, Serializable {

    /**	
     * Member Description
     */
    private static final long serialVersionUID = 6601398807385724350L;

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null) {
            return;
        }

        Object pwd = ((UIInput) component.findComponent("password")).getValue();
        if (pwd == null || !pwd.equals(value)) {
            throw new ValidatorException(createMessage(context, "输入的密码不一致！"));
        }
    }

    private FacesMessage createMessage(FacesContext context, String msgCode, Object... args) {
        return MessageFactory.getMessage(context, msgCode, args);
    }
}
